/* 
 * Song Exporter: ABC
 *
 * Choi Jonghwan<saxboy@saxboy.pe.kr>
 * 2006/04/23
 */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "songexporter.h"

int song_exporter_handler_abc(SONG song)
{
	int i=0; 

	if (song==NULL) return -1; 

	printf("X:1\n"); 
	printf("T:Improviser ABC Test\n"); 
	printf("M:%d/%d\n", song->measure_count, song->measure_base); 
	printf("L:%d/%d\n", 1, song->measure_base * 2); 
	printf("Q:%d/%d=%d\n", 1, song->measure_base, 120); 
	printf("K:C octave=0\n"); 

	for (i=0; i< song_GetVoiceCount(song); i++)
	{
		int x=0; 
		VOICE v= song_GetVoice(song, i); 
		if (v==NULL) continue; 

		/* export drummer */
		if ( v->b_is_drummer )
		{
			printf("%% Drummer --------------------------------- \n"); 
			printf("%%%%MIDI drum %s\n", v->drum_pattern); 
			printf("!drum!\n"); 
			continue; 
		}

		printf("V:%d\n", i); 

		/* print instrument info */
		{
			INSTRUMENT instrument = v->instrument; 

			printf("%%%% %s -------------------- \n", instrument->name); 
			printf("%%%%MIDI program %d\n", instrument->midi_program ); 
			printf("K:C octave=%d\n", instrument->relative_octave ); 
		}

		/* export voice events */
		for ( x=0; x< v->event_count; x++)
		{
			VOICE_EVENT e = v->events[x]; 

			if ( e== VOICE_EVENT_BAR_START )
			{
				printf("| "); 
			}
			else if ( e== VOICE_EVENT_BAR_END )
			{
				/* ignore bar end event */
				printf(" "); 
			}
			//if ( e== VOICE_EVENT_NOTE )
			else
			{
				char abc_note_name[12]={0,}; 
				char abc_note_length_str[12]={0,}; 
				int note_length=0; 

				strcpy(abc_note_name, note_GetStaticAbcString(v->notes[e])); 
				/* FIXME: consider default note length... */

				/* recalc note length in abc's way */
				note_length = note_GetLength(v->notes[e]); 

				switch ( note_length)
				{
				case NOTE_THIRTYSECOND: 
						strcpy( abc_note_length_str, "/4"); 
						break;
				case NOTE_SIXTEENTH: 
						strcpy( abc_note_length_str, "/"); 
						break;
				case NOTE_EIGHTH: 
						strcpy( abc_note_length_str, ""); 
						break;
				case NOTE_DOTTED_EIGHTH: 
						strcpy( abc_note_length_str, "3/2"); 
						break;
				case NOTE_DOUBLE_DOTTED_EIGHTH: 
						strcpy( abc_note_length_str, "7/4"); 
						break;
				case NOTE_QUARTER: 
						strcpy( abc_note_length_str, "2"); 
						break;
				case NOTE_DOTTED_QUARTER: 
						strcpy( abc_note_length_str, "3"); 
						break;
				case NOTE_HALF: 
						strcpy( abc_note_length_str, "4"); 
						break;
				case NOTE_WHOLE:
						strcpy( abc_note_length_str, "8"); 
						break;
				default: 
						strcpy( abc_note_length_str, "*"); 
						break;
				}

				printf("%s%s", abc_note_name, abc_note_length_str); 

			}
		}
		printf("|]\n"); 
	}

}

static int __song_exporter_handler_abc(SONG song)
{
	if (song==NULL) return -1; 

	//printf("Running ABC export\n"); 

	printf("X:1\n"); 
	printf("T:Improviser ABC Test\n"); 
	printf("M:%d/%d\n", song->measure_count, song->measure_base); 
	printf("L:%d/%d\n", 1, song->measure_base * 2); 
	printf("Q:%d/%d=%d\n", 1, song->measure_base, 120); 
	printf("K:C octave=0\n"); 

	/* FIXME: for each style, generate voices */

	srand( time() ); 



	/* drummer */
	//printf("V:1\n"); 
	printf("%% Drummer --------------------------------- \n"); 

#if 1
	/* drummer using abc2midi drum loop */

	/* simple modern jazz */
	printf("%%%%MIDI drum d4d3dd4d3d 51 51 51 51 51 51 \n"); 
	/* simple bossa */
	//printf("%%%%MIDI drum d2d2zdzdzdz2d2d2 61 61 61 61 61 61 61 61 61 61\n"); 
	/* slow shuffle 6/8 */
	//printf("%%%%MIDI drum d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2 38 38 38 38 38 38 38 38 38 38 38 38 38 38 38 38 38 38\n"); 
	printf("!drum!\n"); 
#endif

#if 0
	/* real midi drummer */

	printf("%%%%MIDI channel 9\n"); /* drum */
	//printf("%%%%MIDI program 61\n"); /* sax */
	{
		int i=0; 
		int j=0; 
		for (i=0; i< song->bar_count; i++)
		{
			BAR b = song->bars[i]; 
			printf("| "); 



		}
		printf("|]\n"); 
	}
#endif

	/* sax */
	printf("%% Sax --------------------------------- \n"); 
	printf("V:1\n"); 
	printf("K:C octave=+1\n"); 
	printf("%%%%MIDI program 67\n"); /* sax */
	//printf("%%%%MIDI program 22\n"); /* guitar */
	{
		int i=0; 
		int j=0; 
		for (i=0; i< song->bar_count; i++)
		{
			BAR b = song->bars[i]; 
			printf("| "); 

			//for (j=0; j< b->measure_count; j++)
			{
				CHORD c = bar_GetChord(b, j); 
				CHORD prev_c=NULL; 

				char root[12]={0,}; 
				char third[12]={0,}; 
				char fifth[12]={0,}; 
				char seventh[12]={0,}; 

				if ( c==NULL) continue; 

				strcpy(root, note_GetStaticAbcString(c->notes[0])); 
				strcpy(third, note_GetStaticAbcString(c->notes[1])); 
				strcpy(fifth, note_GetStaticAbcString(c->notes[2])); 
				strcpy(seventh, note_GetStaticAbcString(c->notes[3])); 

				if ( i % 3 == 0 )
				{
					//printf("%s<<%s%s10", third, root, root); 

#if 1
					printf("!mp! "); 
					//printf("%%%%MIDI program 62\n"); 
					printf("%s", third); 
					printf("%s2", root); 
					printf("%s2", fifth); 
					printf("%s", seventh); 
					printf("%s2", root); 
#endif
				}
				else if ( i % 3 == 1 )
				{
					int xx=0; 
				
#if 1
					//printf("%%%%MIDI program 110\n"); 
					printf("!ff! "); 
					for (xx=0; xx<8; xx++)
					{
						char tmp[12]={0,}; 
						strcpy(tmp, note_GetStaticAbcString(c->notes[ rand()%4 ])); 
						printf("%s", tmp); 
					}
#endif 
					//printf("%s<<%s%s10", fifth, root, root); 
	
				}
				else
				{
						char tmp1[12]={0,}; 
						char tmp2[12]={0,}; 
						char tmp3[12]={0,}; 
						//printf("%%%%MIDI program 67\n"); 
						printf("!mf! "); 

						strcpy(tmp1, note_GetStaticAbcString(c->notes[ rand()%4 ])); 
						strcpy(tmp2, note_GetStaticAbcString(c->notes[ rand()%4 ])); 
						strcpy(tmp3, note_GetStaticAbcString(c->notes[ rand()%4 ])); 
						printf("%s/%s/z2%s/%s/", tmp1, tmp2, tmp3, tmp2); 

						strcpy(tmp1, note_GetStaticAbcString(c->notes[ rand()%4 ])); 
						strcpy(tmp2, note_GetStaticAbcString(c->notes[ rand()%4 ])); 
						strcpy(tmp3, note_GetStaticAbcString(c->notes[ rand()%4 ])); 
						printf("%s%s2%s", tmp1, tmp2, tmp3); 

				}


			}
		}
		printf("|]\n"); 
	}


	/* piano */
	printf("%% Piano --------------------------------- \n"); 
	printf("V:2\n"); 
	printf("K:C octave=0\n"); 
	//printf("%%%%MIDI program 24\n"); 
	printf("%%%%MIDI program 1\n"); 

	{
		int i=0; 
		int j=0; 
		for (i=0; i< song->bar_count; i++)
		{
			BAR b = song->bars[i]; 
			printf("| "); 

			//for (j=0; j< b->measure_count; j++)
			{
				CHORD c = bar_GetChord(b, j); 
				CHORD prev_c=NULL; 

				char root[12]={0,}; 
				char third[12]={0,}; 
				char fifth[12]={0,}; 
				char seventh[12]={0,}; 

				if ( c==NULL) continue; 
				strcpy(root, note_GetStaticAbcString(c->notes[0])); 
				strcpy(third, note_GetStaticAbcString(c->notes[1])); 
				strcpy(fifth, note_GetStaticAbcString(c->notes[2])); 
				strcpy(seventh, note_GetStaticAbcString(c->notes[3])); 

#if 1 
				/* simple jazz comping */
				printf("[%s%s%s%s]", root, third, fifth, seventh); 
				printf("z2"); 
				printf("[%s5%s5%s5%s5]", root, third, fifth, seventh); 
#endif

#if 0
				/* 6/8 slow shuffle */
				printf("%%%%MIDI program 22\n"); 
				printf("[%s5%s5%s5%s5]z", root, third, fifth, seventh); 
				printf("[%s1%s1%s1%s1]", root, third, fifth, seventh); 
				printf("[%s1%s1%s1%s1]", root, third, fifth, seventh); 
				printf("z4"); 
#endif

				prev_c = c; 
			}
		}
		printf("|]\n"); 
	}

	/* bass */
	printf("%% Bass --------------------------------- \n"); 
	printf("V:3\n"); 
	printf("%%%%MIDI program 33\n"); 
	printf("K:C octave=-2\n"); 

	{
		int i=0; 
		int j=0; 
		for (i=0; i< song->bar_count; i++)
		{
			BAR b = song->bars[i]; 
			printf("| "); 

			//for (j=0; j< b->measure_count; j++)
			{
				CHORD c = bar_GetChord(b, j); 
				CHORD prev_c=NULL; 

				char root[12]={0,}; 
				char third[12]={0,}; 
				char fifth[12]={0,}; 
				char seventh[12]={0,}; 

				if ( c==NULL) continue; 
				strcpy(root, note_GetStaticAbcString(c->notes[0])); 
				strcpy(third, note_GetStaticAbcString(c->notes[1])); 
				strcpy(fifth, note_GetStaticAbcString(c->notes[2])); 
				strcpy(seventh, note_GetStaticAbcString(c->notes[3])); 

#if 1
				/* simple walking */
				printf("(%s>>%s)", root, root ); 
				printf("%s2", third); 
				printf("%s2", seventh); 
				printf("%s2", fifth); 
#endif

#if 0
				/* simple bossa/samba */
				printf("%s2", root); 
				printf("%s2", fifth); 
				printf("(%s>>%s)", root, fifth ); 
				printf("%s", fifth); 
				printf("%s", root); 
#endif
	
#if 0
				/* simple rock bass */
				{
					int xx=0; 
					for (xx=0; xx<8; xx++)
						printf("%s", root); 
				}
#endif

#if 0
				/* 6/8 slow shuffle */
				{
					int xx=0; 
					for (xx=0; xx<2; xx++)
						printf("%s4z2", root); 
					
				}
#endif

			}

		}
		printf("|]\n"); 
	}

	return 1; 
}


